PrepareLevel2:
	;; position of twoplayer scoreboard
	;; VRAM C0BA
	move.l	#$40BA0003,d7
	bsr	PrepareLevelCommon

	btst	#TwoPlayerFlag,Flags
	bne.s	PrepareLevel2TwoPlayer

PrepareLevel2OnePlayer:	

	;; copy ground tiles
	move.w	#39,d0
	;; VRAM CD00
	move.l	#$4D000003,(a4)
Level2GroundMapLoop:	
	move.w	#($80+(StartBORDER2Tiles-StartTiles)/32+%1110),(a3)
	dbf	d0,Level2GroundMapLoop

	;; copy ground tiles
	move.w	#39,d0
	;; VRAM CD80
	move.l	#$4D800003,(a4)
Level2GroundMapLoop2:	
	move.w	#($80+(StartBORDER2Tiles-StartTiles)/32+%1101),(a3)
	dbf	d0,Level2GroundMapLoop2
	
	lea	StartSpriteLevel2Initial(pc),a5
	;; sprite load follows branch
	bra.s	PrepareLevel2Final
	
PrepareLevel2TwoPlayer:	

	;; VRAM CD00
	move.l	#$4D000003,(a4)

	;; copy ground tiles
	move.w	#18,d0
Level2GroundMapLoop3:	
	move.w	#($80+(StartBORDER2Tiles-StartTiles)/32+%1110),(a3)
	dbf	d0,Level2GroundMapLoop3
	
	move.w	#($80+(StartBORDER2Tiles-StartTiles)/32+%0110),(a3)
	move.w	#($2080+(StartBORDER2Tiles-StartTiles)/32+%1010),(a3)

	;; copy ground tiles
	move.w	#18,d0
Level2GroundMapLoop4:	
	move.w	#($2080+(StartBORDER2Tiles-StartTiles)/32+%1110),(a3)
	dbf	d0,Level2GroundMapLoop4

	;; VRAM CD80
	move.l	#$4D800003,(a4)

	;; copy ground tiles
	move.w	#18,d0
Level2GroundMapLoop5:	
	move.w	#($80+(StartBORDER2Tiles-StartTiles)/32+%1101),(a3)
	dbf	d0,Level2GroundMapLoop5

	move.w	#($80+(StartBORDER2Tiles-StartTiles)/32+%0101),(a3)
	move.w	#($2080+(StartBORDER2Tiles-StartTiles)/32+%1001),(a3)

	;; copy ground tiles
	move.w	#18,d0
Level2GroundMapLoop6:	
	move.w	#($2080+(StartBORDER2Tiles-StartTiles)/32+%1101),(a3)
	dbf	d0,Level2GroundMapLoop6

	lea	StartSpriteLevel2TwoPlayerInitial(pc),a5
	
PrepareLevel2Final:	
	bsr	SpriteLoad

	lea	Level2Animate(pc),a5
	move.l	a5,VintAnimate

	bsr	Level2RefreshLetters
	bsr	FadeIn
	
Level2Loop:	
	bsr	WaitForVint

	;; demo mode check
	btst	#DemoFlag,Flags
	beq.s	Level2SkipDemoMode
	;; in demo mode, exit on button press
	btst	#StartButtonFlag,Flags
	bne	PrepareTitleFadeOut
	;; demo only shows for a certain amount of frames
	cmpi.w	#VintWaitDemo,VintWait
	bge	PrepareTitleFadeOut
	;; determine where to move demo Riley
	clr.b	d1
	move.w	RileySprite+6,d0
	addq.w	#$0006,d0
	cmp.w	LetterSprite+6,d0
	bgt.s	Level2DemoMoveLeft
	addq.w	#$0004,d0
	cmp.w	LetterSprite+6,d0
	blt.s	Level2DemoMoveRight
	bra.s	Level2DemoUpdatePorts
Level2DemoMoveLeft:	
	bset	#bitLeft,d1
	bra.s	Level2DemoUpdatePorts
Level2DemoMoveRight:	
	bset	#bitRight,d1
Level2DemoUpdatePorts:
	;; since game boards are symetrical, apply same input to both ports
	move.b	d1,Port1Hold
	move.b	d1,Port2Hold
Level2SkipDemoMode:	

	;; check for sprite collision
	move.w   (a4),d0
	andi.b   #$0020,d0
	beq      Level2MoveRiley1
	;; there is a sprite collision
	;; check player 1 letter and riley (bin) collision
	;; see if letter already disabled
	tst.b    LetterSprite+5
	beq.s    Level2Player2Collision
	;; collision check
	lea	RileySprite,a5
	lea	LetterSprite,a6
	bsr	CheckSpriteInsideAnotherSprite
	bne.s	Level2Player2Collision
	;; letter and riley (bin) collision
	;; now update the score
	lea     EndScoreAscii,a5
        move.l  #$40900003,d0
	move.w  #(EndScoreAscii-StartScoreAscii-1),d1
	clr.w	d2
	move.b  WordIndex,d2
	;; make high priority (so score will go above other things)
	move.w  #$8000,d4
	bsr	UpdateAndDisplayScoreAndWord

	;; get next character
	clr.w	d0
	move.b  WordIndex,d0
	clr.w	d1
	move.b  WordIndex2,d1
	bsr	GetNextLetter
	move.b	d0,WordIndex

	clr.b	LetterSprite+5
	move.w	#$0078,LetterSprite

Level2Player2Collision:	
	btst	#TwoPlayerFlag,Flags
	beq.s	Level2CheckRefreshLetters
	;; see if letter already $00 (end of word)
	tst.b    Letter2Sprite+5
	beq.s    Level2CheckRefreshLetters
	;; collision check
	lea	Riley2Sprite,a5
	lea	Letter2Sprite,a6
	bsr	CheckSpriteInsideAnotherSprite
	bne.s	Level2CheckRefreshLetters
	;; now update the score ($30-$39 are valid ASCII numbers)
	lea     EndScoreAscii2,a5
	move.l	#$40C60003,d0
	move.w  #(EndScoreAscii2-StartScoreAscii2-1),d1
	clr.w	d2
	move.b  WordIndex2,d2
	;; make high priority (so score will go above other things)
	move.w  #$A000,d4
	bsr	UpdateAndDisplayScoreAndWord

	;; get next character
	clr.w	d0
	move.b  WordIndex2,d0
	clr.w	d1
	move.b  WordIndex,d1
	bsr	GetNextLetter
	move.b	d0,WordIndex2

	clr.b	Letter2Sprite+5
	move.w	#$0078,Letter2Sprite

Level2CheckRefreshLetters:	
	bclr	#RefreshLettersWordFlag,WordFlags
	beq.s	Level2CheckNewWord
	bsr	Level2RefreshLetters
	
Level2CheckNewWord:	
	bclr	#NewWordFlag,WordFlags
	beq.s	Level2CheckNewLevel
	;; set y positions of letters to same starting point
	move.w	#$0078,LetterSprite
	move.w	#$0078,Letter2Sprite

Level2CheckNewLevel:	
	;; check if need to go to next level
	;; demo cannot go to another level
	bclr	#NewLevelFlag,WordFlags
	beq.s	Level2MoveRiley1
	btst	#DemoFlag,Flags
	bne.s	Level2MoveRiley1
	bsr	LevelTransition
	bra	PrepareLevel3

Level2MoveRiley1:
	move.b	Port1Hold,d0
	lea	RileySprite,a5
	bsr.s	Level2MoveRiley
Level2MoveRiley2:
	move.b	Port2Hold,d0
	lea	Riley2Sprite,a5
	bsr.s	Level2MoveRiley
	
	bra	Level2Loop


	;; subroutines for level 3

	
Level2MoveRiley:
	move.w	6(a5),d1
	btst	#bitLeft,d0
	bne.s	Level2MoveLeft
	btst	#bitRight,d0
	bne.s	Level2MoveRight
	bra.s	Level2FinishMove
Level2MoveLeft:
	subq.w	#1,d1
	cmpi.w	#$80,d1
	bge.s	Level2FinishMove
	move.w	#$80,d1
	bra.s	Level2FinishMove
Level2MoveRight:	
	addq.w	#1,d1
	cmpi.w	#($80+40*8-24),d1
	ble.s	Level2FinishMove
	move.w	#($80+40*8-24),d1
Level2FinishMove:
	move.w	d1,6(a5)
	addq.w	#4,d1
	move.w	d1,30(a5)
	rts


	
Level2Animate:
	;; move letter down
	move.w	 LetterSprite,d0
	addq.w	 #1,d0
	cmpi.w	 #($80+8*28),d0
	blt.s	 Level2SkipResetLetterY
	;; wrap around the letter to the right side of the screen
	move.w	 #$0078,d0
Level2SkipResetLetterY:	
	move.w	 d0,LetterSprite

	;; move letter down
	move.w	 Letter2Sprite,d0
	addq.w	 #1,d0
	cmpi.w	 #($80+8*28),d0
	blt.s	 Level2SkipResetLetter2Y
	;; wrap around the letter to the right side of the screen
	move.w	 #$0078,d0
Level2SkipResetLetter2Y:	
	move.w	 d0,Letter2Sprite

	rts

	
	;; updates letters, and for level 1 the letter x and y positions
	;; no inputs
	;; destroys a5
Level2RefreshLetters:	
	bsr	RefreshLetters

	movem.w d0-d3,-(a7)
	;; update letter x location
	lea	StartWordX,a5

	clr.w	d0
	clr.w	d2
	move.b	WordIndex,d0
	move.b	WordIndex2,d2
	move.b	(a5,d0.w),d1
	move.b	(a5,d2.w),d3
	;; in two player mode, have less x space to use
	btst	#TwoPlayerFlag,Flags
	bne.s	Level2RefreshLetterPositionsTwoPlayerX

	andi.w  #$00FF,d1
	;; $80 + (320/2 - 256/2 - 8/2)
	addi.w  #$009C,d1
	move.w  d1,LetterSprite+6
	bra.s 	Level2RefreshLetterPositionsY

Level2RefreshLetterPositionsTwoPlayerX:
	andi.w  #$007F,d1
	andi.w  #$007F,d3
	;; $80 + (320/4 - 128/2 - 8/2)
	addi.w  #$008C,d1
	addi.w  #$012C,d3
	move.w  d1,LetterSprite+6
	move.w  d3,Letter2Sprite+6

Level2RefreshLetterPositionsY:	

	movem.w (a7)+,d0-d3
	rts


;;; data used for level 3


StartSpriteLevel2Initial:
	;; Riley sprite is really the bin sprite and vice versa
RileySpriteLevel2Initial:
	dc.b $01,$40,$09,$04,$00,$9E,$01,$14
	dc.b $00,$00,$00,$00,$00,$00,$00,$00
	dc.b $00,$00,$00,$00,$00,$00,$00,$00
BinSpriteLevel2Initial:
	dc.b $01,$36,$06,$00,$48,$80,$01,$18
LetterSpriteLevel2Initial:
	dc.b $00,$70,$00,$03,$00,$00,$01,$1C
EndSpriteLevel2Initial:	


StartSpriteLevel2TwoPlayerInitial:
	;; Riley sprite is really the bin sprite and vice versa
RileySpriteLevel2TwoPlayerInitial:
	dc.b $01,$40,$09,$04,$00,$9E,$00,$C4
	dc.b $00,$00,$00,$00,$00,$00,$00,$00
	dc.b $00,$00,$00,$00,$00,$00,$00,$00
BinSpriteLevel2TwoPlayerInitial:
	dc.b $01,$36,$06,$05,$48,$80,$00,$C8
LetterSpriteLevel2TwoPlayerInitial:
	dc.b $00,$70,$00,$03,$00,$00,$01,$1C
Riley2SpriteLevel2TwoPlayerInitial:
	dc.b $01,$40,$09,$09,$28,$9E,$01,$64
	dc.b $00,$00,$00,$00,$00,$00,$00,$00
	dc.b $00,$00,$00,$00,$00,$00,$00,$00
Bin2SpriteLevel2TwoPlayerInitial:
	dc.b $01,$36,$06,$00,$68,$80,$01,$68
Letter2SpriteLevel2TwoPlayerInitial:
	dc.b $00,$70,$00,$08,$20,$00,$01,$6C
EndSpriteLevel2TwoPlayerInitial:	

